package com.china08.yxyapi.api.home.activity.declaration;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.huodong.declaration.YxyHuodongHrbDeclaration;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.declaration.YxyHuodongHrbDeclarationRepository;

/**
 * 
 * 哈尔滨教师宣言
 *
 */
@CrossOrigin
@RestController
public class ActivityHrbTeacherDeclarationController extends YxyApiBaseController {

    @Autowired
    private YxyHuodongHrbDeclarationRepository yxyHuodongHrbDeclarationRepository;

    @RequestMapping(value = "/api_yw/home/activity/hrb/teacher/declaration/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String userId = getProfile(request, response).getId();
        String roleId = getProfile(request, response).getRoleId();
        String schoolId = getProfile(request, response).getSchoolId();
        Integer loginStatus = 0;
        Integer status = 0;
        Integer noDecl = 0;
        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");
        Page<YxyHuodongHrbDeclaration> page = null;

        // 查询教师宣言
        page = yxyHuodongHrbDeclarationRepository.findByDeclarationNotNull(pageable);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd   hh:mm");
        List<RespModel> list = new ArrayList<RespModel>();
        if (null != page.getContent() && page.getContent().size() > 0) {
            noDecl = 1;
            Integer aa = (int) page.getTotalElements();
            Integer countNum = aa;
            for (YxyHuodongHrbDeclaration yxyHuodongHrbDeclaration : page) {
                RespModel model = new RespModel();
                model.declaration = yxyHuodongHrbDeclaration.getDeclaration();
                model.teacherId = yxyHuodongHrbDeclaration.getTeacherId();
                model.teacherNick = yxyHuodongHrbDeclaration.getTeacherNick();
                model.createDate = dateFormat.format(yxyHuodongHrbDeclaration.getLastModifiedDate());
                model.isBest = yxyHuodongHrbDeclaration.getIsBest();
                model.schoolNick = "( " + yxyHuodongHrbDeclaration.getSchoolNick() + " )";
                model.schoolId = yxyHuodongHrbDeclaration.getSchoolId();
                // 判断是否登录
                if (null == userId || !(userId.length() > 0)) {
                    loginStatus = 0;
                } else {
                    loginStatus = 1;
                }
                // 判断身份
                if (null != roleId && "" != roleId) {
                    // 教师
                    if (roleId.equals("ROLE_TEACHER")) {
                        status = 0;
                    } else
                    // 教育局管理员
                    if (roleId.equals("ROLE_EDU_ADMIN")) {
                        status = 1;
                    } else if (roleId.equals("ROLE_SCH_ADMIN")) {
                        status = 2;
                    } else {
                        status = 3;
                    }
                }
                // 我是第几位
                if (null != schoolId && "" != schoolId) {
                    YxyHuodongHrbDeclaration teacherIdAndSchoolId = yxyHuodongHrbDeclarationRepository
                            .findByTeacherId(userId);
                    if (null != teacherIdAndSchoolId) {
                        model.orderNum = teacherIdAndSchoolId.getOrderNum();
                    } else {
                        model.orderNum = 0;
                    }
                } else {
                    model.orderNum = 0;
                }
                model.status = status;
                model.loginStatus = loginStatus;
                model.countNum = countNum;
                model.noDecl = noDecl;
                list.add(model);
            }
        } else {
            noDecl = 0;
            RespModel model = new RespModel();
            // 判断是否登录
            if (null == userId || !(userId.length() > 0)) {
                loginStatus = 0;
            } else {
                loginStatus = 1;
            }
            // 判断身份
            if (null != roleId && "" != roleId) {
                // 教师
                if (roleId.equals("ROLE_TEACHER")) {
                    status = 0;
                } else
                // 教育局管理员
                if (roleId.equals("ROLE_EDU_ADMIN")) {
                    status = 1;
                } else if (roleId.equals("ROLE_SCH_ADMIN")) {
                    status = 2;
                } else {
                    status = 3;
                }
            }
            // 我是第几位
            if (null != schoolId && "" != schoolId) {
                YxyHuodongHrbDeclaration teacherIdAndSchoolId = yxyHuodongHrbDeclarationRepository
                        .findByTeacherId(userId);
                if (null != teacherIdAndSchoolId) {
                    model.orderNum = teacherIdAndSchoolId.getOrderNum();
                } else {
                    model.orderNum = 0;
                }
            } else {
                model.orderNum = 0;
            }
            model.declaration = "";
            model.teacherId = "";
            model.teacherNick = "";
            model.createDate = "";
            model.isBest = 0;
            model.schoolNick = "";
            model.schoolId = "";
            model.orderNum = 0;
            model.status = status;
            model.loginStatus = loginStatus;
            model.countNum = 0;
            model.noDecl = noDecl;
            list.add(model);
        }
        Page<RespModel> pageRes = new PageImpl<RespModel>(list, pageable, page.getTotalElements());

        return pageRes;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

    }

    protected static class RespModel extends YxyApiRespModel {
        // 0 未登录 1 登录
        public Integer loginStatus;
        // 宣誓总人数
        public Integer countNum;
        // 教师宣言
        public String declaration;
        // 教师名称
        public String teacherNick;
        // 教师ID
        public String teacherId;
        // 创建时间
        public String createDate;
        // 学校名称
        public String schoolNick;
        // 登录身份 0教师 1教育局管理员 2 学校管理员角色
        public Integer status;
        // 我是第几个宣言人
        public Integer orderNum;
        // 是否精品宣言
        public Integer isBest;
        public String schoolId;
        // 没有宣言
        public Integer noDecl;
    }

}
